package service

import (
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
	"time"
	"xianzhi/models"
	"xianzhi/utils"
)

// 获取会员信息
func GetUserInfo(p map[string]interface{}) *models.User {
	o := orm.NewOrm()
	user := &models.User{}
	qs := o.QueryTable("User")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["openId"]; ok {
		qs = qs.Filter("open_id", p["openId"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("phone", p["phone"])
	}
	qs.One(user)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if user.Id > 0 {
			if len(user.Phone) > 0 {
				user.PhoneShow = utils.FormatMobile(user.Phone)
			}
			user.IsReceiveWechatNoticeShow = models.UserIsReceiveWechatNotice[user.IsReceiveWechatNotice]
			user.SubscribeStatusShow = models.UserSubscribeStatus[user.SubscribeStatus]
			user.SubscribeTimeShow = user.SubscribeTime.Format("2006-01-02 15:04:05")
			user.UnsubscribeTimeShow = user.UnsubscribeTime.Format("2006-01-02 15:04:05")
			user.CreatedShow = user.Created.Format("2006-01-02 15:04:05")
			user.UpdatedShow = user.Updated.Format("2006-01-02 15:04:05")
		}
	}
	return user
}

// 添加会员信息
func AddUser(input url.Values) (int64, error) {
	o := orm.NewOrm()
	user := &models.User{}
	if _, ok := input["open_id"]; ok {
		user.OpenId = input["open_id"][0]
	}
	if _, ok := input["union_id"]; ok {
		user.UnionId = input["union_id"][0]
	}
	if _, ok := input["school_name"]; ok {
		user.SchoolName = input["school_name"][0]
	}
	if _, ok := input["phone"]; ok {
		user.Phone = input["phone"][0]
	}
	if _, ok := input["password"]; ok {
		user.Password = utils.Encode(input["password"][0])
	}
	if _, ok := input["nick_name"]; ok {
		user.NickName = input["nick_name"][0]
	}
	if _, ok := input["head_img_url"]; ok {
		user.HeadImgUrl = input["head_img_url"][0]
	}
	if _, ok := input["status"]; ok {
		user.Status, _ = strconv.Atoi(input["status"][0])
	}
	if _, ok := input["subscribe_status"]; ok {
		user.SubscribeStatus, _ = strconv.Atoi(input["subscribe_status"][0])
	}
	if _, ok := input["is_receive_wechat_notice"]; ok {
		user.IsReceiveWechatNotice, _ = strconv.Atoi(input["is_receive_wechat_notice"][0])
	}
	if _, ok := input["subscribe_time"]; ok {
		loc, _ := time.LoadLocation("Local")
		theTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input["subscribe_time"][0], loc)
		user.SubscribeTime = theTime
	}
	return o.Insert(user)
}

// 修改会员信息
func EditUser(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		if k == "password" {
			if len(v[0]) > 0 {
				p[k] = utils.Encode(v[0])
			}
			continue
		}
		p[k] = v[0]
	}
	p["updated"] = time.Now().Format("2006-01-02 15:04:05")
	return o.QueryTable("User").Filter("id", input["id"][0]).Update(p)
}

// 获取会员列表
func GetUserList(p map[string]interface{}) (int64, []*models.User) {
	var userList []*models.User
	o := orm.NewOrm()
	qs := o.QueryTable("User")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["schoolName"]; ok {
		qs = qs.Filter("school_name__contains", p["schoolName"])
	}
	if _, ok := p["nickName"]; ok {
		qs = qs.Filter("nick_name__contains", p["nickName"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("phone__contains", p["phone"])
	}
	if _, ok := p["subscribeStatus"]; ok {
		qs = qs.Filter("subscribe_status", p["subscribeStatus"])
	}
	if _, ok := p["isReceiveWechatNotice"]; ok {
		qs = qs.Filter("is_receive_wechat_notice", p["isReceiveWechatNotice"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-created").All(&userList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, user := range userList {
			user.StatusShow = models.UserStatus[user.Status]
			user.IsReceiveWechatNoticeShow = models.UserIsReceiveWechatNotice[user.IsReceiveWechatNotice]
			user.SubscribeStatusShow = models.UserSubscribeStatus[user.SubscribeStatus]
			user.SubscribeTimeShow = user.SubscribeTime.Format("2006-01-02 15:04:05")
			user.UnsubscribeTimeShow = user.UnsubscribeTime.Format("2006-01-02 15:04:05")
			user.CreatedShow = user.Created.Format("2006-01-02 15:04:05")
			user.UpdatedShow = user.Updated.Format("2006-01-02 15:04:05")
		}
	}
	return totalCount, userList
}